package com.tjsiqi.service;

import com.tjsiqi.Vo.JbzInfoMVo;
import com.tjsiqi.Vo.OutJbzCountVo;
import com.tjsiqi.beans.PageQuery;
import com.tjsiqi.beans.PageResult;
import com.tjsiqi.dao.WebusercontrolMapper;
import com.tjsiqi.model.Webusercontrol;
import com.tjsiqi.param.SearchJbzInfoParam;
import com.tjsiqi.util.DateTimeUtil;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.validator.constraints.EAN;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class WebUserControlService {
    @Resource
    private WebusercontrolMapper webusercontrolMapper;
    //  保存注册用户信息
    public void save(Webusercontrol webusercontrol) {
        webusercontrolMapper.insertSelective(webusercontrol);
    }

    public Webusercontrol getJbzInfo(String jbzCode) {
        return webusercontrolMapper.selectByPrimaryKey(jbzCode);
    }

    public void update(Webusercontrol webusercontrol){
        webusercontrolMapper.updateByPrimaryKeySelective(webusercontrol);
    }

    //管理员查询搅拌站信息
    public PageResult<JbzInfoMVo> getJbzInfoMs(SearchJbzInfoParam searchJbzInfoParam, PageQuery page) {

        if (StringUtils.isNotBlank(searchJbzInfoParam.getMc())) {
            searchJbzInfoParam.setMc("%" + searchJbzInfoParam.getMc() + "%");
        }else{
            searchJbzInfoParam.setMc(null);
        }
        int count = webusercontrolMapper.countByParam(searchJbzInfoParam);
        if(count >0) {
            List<JbzInfoMVo> jbzInfoMVos = webusercontrolMapper.getJbzInfoMs(searchJbzInfoParam,page);
            return PageResult.<JbzInfoMVo>builder().total(count).data(jbzInfoMVos).build();
        }
        return PageResult.<JbzInfoMVo>builder().build();
    }

    //查询到期搅拌站数
    public OutJbzCountVo getOutJbzCount() {
        OutJbzCountVo outJbzCountVo = new OutJbzCountVo();
        Calendar c = Calendar.getInstance();
        c.add(Calendar.YEAR, -1);
        int outJbzCount = webusercontrolMapper.getoutJbzCount(c.getTime());
        outJbzCountVo.setOutJbzCount(outJbzCount);
        c.add(Calendar.DATE,10);
        int nearOutJbzCount =(webusercontrolMapper.getNearOutJbzCount(c.getTime()) - outJbzCount);
        outJbzCountVo.setNearOutJbzCount(nearOutJbzCount);
        System.out.println("OutJbzCountSum"+outJbzCountVo.getOutJbzCountSum()+"ou"+outJbzCountVo.getOutJbzCount()+"ne"+outJbzCountVo.getNearOutJbzCount());
        outJbzCountVo.setOutJbzCountSum(outJbzCount+nearOutJbzCount);
        System.out.println("OutJbzCountSum1"+outJbzCountVo.getOutJbzCountSum());
        return outJbzCountVo;
    }

    //搅拌站续期
    public int updateJbzByCode(Webusercontrol webusercontrol) {
        int isUpdate = webusercontrolMapper.updateByPrimaryKeySelective(webusercontrol);
        System.out.println("isupdate是否更1新成功"+isUpdate);
        return isUpdate;
    }

    public JbzInfoMVo findByCode(String jbzCode) {
        JbzInfoMVo jbzInfoMVo = webusercontrolMapper.selectByCode(jbzCode);
        return  jbzInfoMVo;
    }
}
